﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace RRSimulator.BlobManager.ServiceHost
{
    public class Global : System.Web.HttpApplication
    {
        private IList<UserModel> _userList = new List<UserModel>()
        {
            new UserModel{UserName = "yusup", Password = "@accel4me", Roles = "Read,Write"},
            new UserModel{UserName = "afisher", Password = "@accel4me", Roles = "Read"}
        };

        protected void Application_Start(object sender, EventArgs e)
        {
            System.Web.ApplicationServices.AuthenticationService.Authenticating +=
                new EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs>(
                AuthenticationService_Authenticating);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        private void AuthenticationService_Authenticating(object sender,
         System.Web.ApplicationServices.AuthenticatingEventArgs e)
        {
            var user = _userList.FirstOrDefault(u => u.UserName == e.UserName && u.Password == e.Password);
            e.Authenticated = user != null;
            e.AuthenticationIsComplete = true;

            if (e.Authenticated)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,
                    e.UserName,
                    DateTime.Now,
                    DateTime.Now.AddHours(24),
                    true,
                    user.Roles,
                    FormsAuthentication.FormsCookiePath);

                // Encrypt the ticket using machine key
                string encryptedValue = FormsAuthentication.Encrypt(ticket);

                // Attach Cookie to Operation Context header
                HttpResponseMessageProperty response = new HttpResponseMessageProperty();
                response.Headers[HttpResponseHeader.SetCookie] = FormsAuthentication.FormsCookieName + "=" + encryptedValue;
                OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = response;
            }
        }
    }

    public class UserModel
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Roles { get; set; }
    }
}